import {defineStore} from "pinia";
import {reactive, ref} from "vue";
import {reqAttr, reqC1, reqC2, reqC3} from "../../api/product/attr";
import type {Records} from "../../api/product/spu/type.ts";

export default defineStore("Category", () => {
    let c1Arr = reactive([]);
    let c2Arr = reactive([]);
    let c3Arr = reactive([]);
    let attrArr = reactive([]);
    let c1Id = ref<string | number>('');
    let c2Id = ref<string | number>('');
    let c3Id = ref<string | number>('');
    let spuId = ref<string | number>();
    let spuArr = reactive<Records>([]);

    async function getC1() {
        const result = await reqC1();
        if (result.code === 200) {
            Object.assign(c1Arr, result.data);
        }
    }

    async function getC2() {
        const result = await reqC2(c1Id.value);
        if (result.code === 200) {
            Object.assign(c2Arr, result.data);
        }
    }

    async function getC3() {
        const result = await reqC3(c2Id.value);
        if (result.code === 200) {
            Object.assign(c3Arr, result.data);
        }
    }

    async function getAttr() {
        const result = await reqAttr(c1Id.value, c2Id.value, c3Id.value);
        if (result.code === 200) {
            Object.assign(attrArr, result.data);
        }
    }

    // 添加 reset 方法来替代 $reset
    function $reset() {
        c1Arr.length = 0;
        c2Arr.length = 0;
        c3Arr.length = 0;
        attrArr.length = 0;
        c1Id.value = '';
        c2Id.value = '';
        c3Id.value = '';
        spuArr.length = 0;
        spuId.value = '';
    }

    return {
        c1Arr, c1Id, getC1,
        c2Arr, c2Id, getC2,
        c3Arr, c3Id, getC3,
        attrArr, getAttr,
        $reset,
        spuArr, spuId
    }
});